<template>
    <div class="box">
        <el-row>
            <el-col :span="22" :push="1">
                <div class="grid-content bg-purple" style="height: 77px">
                    <span style="line-height: 77px; font-size: 14px"> </span>
                    <span style="margin-left: 20px">
                        <el-button type="primary" size="small" @click="addCar">添加车辆</el-button>
                        <el-button class="file-box" text>
                            <input type="file"  class="file-btn"
                                ref="file"
                                @click="e => {e.target.value = '';} "
                                @change="excelpimport"/>导入  
                        </el-button>
                        <el-button size="small" @click="excelexport">导出</el-button>
                    </span>
                </div>
            </el-col>
        </el-row>
        <!-- 搜索栏start -->
        <el-row>
            <el-col :span="22" :push="1">
                <div class="grid-content bg-purple" style="height: 77px">
                    <div style="width: 100%; margin-left: 20px;">
                        <span style="margin-left: 20px;">
                            <el-form :inline="true" :model="formInline" size="mini" class="demo-form-inline">
                                <!-- &nbsp;&nbsp;&nbsp;&nbsp; -->
                                <el-form-item>
                                    <el-input v-model="formInline.carName" placeholder="请输入车辆名称"></el-input>
                                </el-form-item>
                                <el-form-item label="车牌号">
                                    <el-select v-model="formInline.carNumber" placeholder="车牌号">
                                        <span v-for="(item, index) in carNumbers" :key="index">
                                            <el-option :label="item.carNumber" :value="item.carNumber"></el-option>
                                        </span>
                                    </el-select>
                                </el-form-item>
                                <el-form-item label="车辆状态">
                                    <el-select v-model="formInline.carState" placeholder="车辆状态">
                                        <span v-for="(item, index) in carStates" :key="index">
                                            <el-option :label="item.label" :value="item.value"></el-option>
                                        </span>
                                    </el-select>
                                </el-form-item>
                                <el-form-item>
                                    <el-button type="primary" @click="onSubmit">搜索</el-button>
                                    <el-button type="primary" @click="reset">重置</el-button>
                                </el-form-item>
                            </el-form>
                        </span>
                    </div>
                </div>
            </el-col>
        </el-row>
        <!-- 搜索栏end -->
        <!-- 表格 -->
        <el-row>
            <el-col :span="22" :push="1">
                <div class="grid-content bg-purple">
                    <span style="padding-left: 10px">
                        <el-table :data="tableData" @selection-change="handleSelectionChange" border
                            style="width: 96.5%; margin-left: 20px">
                            <el-table-column type="selection" width="55">
                            </el-table-column>
                            <el-table-column prop="carImg" label="车辆图片" width="160">
                                <template slot-scope="scope">
                                    <img :src="scope.row.carImg" width="150px">
                                </template>
                            </el-table-column>
                            <el-table-column prop="carName" label="车辆名称" width="170">
                            </el-table-column>
                            <el-table-column prop="carNumber" label="车牌号" width="100">
                            </el-table-column>
                            <el-table-column prop="seating" label="座位数" width="140">
                            </el-table-column>
                            <el-table-column prop="carState" label="车辆状态" width="140">
                                <template slot-scope="scope">
                                    <span v-if="scope.row.carState == '0'"><el-tag effect="dark">空闲</el-tag></span>
                                    <span v-if="scope.row.carState == '1'"><el-tag effect="dark"
                                            type="success">使用中</el-tag></span>
                                    <span v-if="scope.row.carState == '2'"><el-tag effect="dark"
                                            type="danger">审批拒绝</el-tag></span>
                                    <span v-if="scope.row.carState == '3'"><el-tag effect="dark"
                                            type="warning">审批撤销</el-tag></span>
                                </template>
                            </el-table-column>
                            <el-table-column prop="carRemark" label="备注" width="140">
                            </el-table-column>
                            <el-table-column label="操作" width="140">
                                <template slot-scope="scope">
                                    <el-button @click="detailsClick(scope.row.id, true)" type="text"
                                        size="small">详情</el-button>
                                    <el-button @click="deleteClick(scope.row.id)" type="text"
                                        size="small">删除</el-button>
                                    <el-button @click="updateClick(scope.row.id)" type="text"
                                        size="small">修改</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                        <div class="grid-content bg-purple-dark">
                            <div class="block">
                                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                                    :current-page="pageNum" :page-sizes="[5, 10, 20, 50]" :page-size="pageSize"
                                    layout="total, sizes, prev, pager, next, jumper" :total="total">
                                </el-pagination>
                            </div>
                        </div>
                    </span>
                </div>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import axios from 'axios'
export default {
    data() {
        return {
            formInline: {},
            pageNum: 1,
            pageSize: 5,
            total: 1,
            tableData: [],
            carNumbers: [],
            carStates: [{ value: "0", label: "空闲" },
            { value: "1", label: "使用中" },
            { value: "2", label: "审核拒绝" },
            { value: "3", label: "审核撤销" }]
        }
    },
    created() {
        this.getCarNumber()
        this.getCarList()
    },
    methods: {
        //添加
        addCar() {
            this.$router.push("/vehicleoperation")
        },
        //详情
        detailsClick(id, flag) {
            this.$router.push({ path: "/vehicleoperation", query: { id: id, flag: flag } },)
        },
        //修改
        updateClick(row) {
            this.$router.push({ path: "/vehicleoperation", query: { id: row } },)
        },
        //删除
        deleteClick(id) {
            axios.post("/car/deleteCarById?id=" + id).then((res) => {
                console.log(res)
                if (res.data.code === 200) {
                    this.$message.success(res.data.data)
                    this.getCarList()
                } else {
                    this.$message.error("操作失败")
                }
            })
        },
        //搜索
        onSubmit() {
            this.getCarList()
        },
        //查询车牌号
        getCarNumber() {
            axios.get("/car/getCarNumber").then((res) => {
                console.log(res)
                this.carNumbers = res.data.data
            })
        },
        //查询车辆信息
        getCarList() {
            axios.post("/car/getCar?pageSize=" + this.pageSize + "&pageNum=" + this.pageNum, this.formInline).then((res) => {
                console.log(res)
                this.tableData = res.data.data.records
                this.total = res.data.data.total
            })
        },
        //重置
        reset() {
            this.formInline = {}
            this.getCarList()
        },
        // 导入
        excelpimport() {
            const inputFile = this.$refs.file.files[0];
            console.log(inputFile);
            var formDate = new FormData();
            formDate.set("file", inputFile);
            this.$http
                .post("car/excelpimport", formDate, {
                    headers: {
                        "Content-Type": "multipart/form-data",
                    },
                })
                .then((res) => {
                    if (res.data.code === 200) {
                        this.$message(res.data.data);
                        this.getCarList();
                    } else {
                        this.$message.error(res.data.data);
                    }
                });

        },
        // 导出
        excelexport() {
            this.$http({
                url: "car/excelexport",
                method: "GET",
                responseType: "arraybuffer", // 接收二进制文件流
            }).then((res) => {
                const content = res.data;
                const blob = new Blob([content], { type: "application/octet-stream" });
                const fileName = "车辆信息表格.xlsx";

                if (typeof window.navigator.msSaveBlob !== "undefined") {
                    window.navigator.msSaveBlob(blob, fileName);
                } else {
                    const fileUrl = window.URL.createObjectURL(blob);
                    const a = document.createElement("a");
                    a.href = fileUrl;
                    a.target = "_blank";
                    a.download = fileName;
                    document.body.appendChild(a);
                    a.click();
                    document.body.removeChild(a);
                }
            });
        },
        //每页条数改变时触发 选择一页显示多少行
        handleSizeChange(val) {
            this.pageSize = val;
            this.getCarList();
        },
        //当前页改变时触发 跳转其他页
        handleCurrentChange(val) {
            this.pageNum = val;
            this.getCarList();
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        }
    }

}
</script>

<style scoped>
/* element-ui样式 */
.el-row {
    margin-bottom: 10px;

    &:last-child {
        margin-bottom: 0;
    }
}

.el-col {
    border-radius: 4px;
}

.bg-purple {
    background: #fff;
    border-radius: 10px;
}

/* 自定义样式 */
/* * {
    margin: 0px;
    padding: 0px;
  } */
.box {
    width: 98%;
    height: 100%;
    margin: auto;
    /* background-color: red; */
}
.file-box {
    display: inline-block;
    position: relative;
    overflow: hidden;
    color: black;
    background-color: rgb(255, 255, 255);
}
 
.file-btn {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    outline: none;
    filter: alpha(opacity=0);
    -moz-opacity: 0;
    -khtml-opacity: 0;
    opacity: 0;
}
</style>